package com.aim.controller.system;

import com.aim.common.annotion.Permission;
import com.aim.common.base.AjaxResult;
import com.aim.common.util.OftenUtil;
import com.aim.common.util.PageUtil;
import com.aim.common.util.WebUtil;
import com.aim.entity.system.SysOperationLog;
import com.aim.service.system.SysOperationLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @AUTO 业务日志控制器
 * @Author AIM
 * @DATE 2019/4/26
 */
@Controller
@RequestMapping("/log")
public class SysLogBizController {

    private static String PREFIX = "system/log/";

    @Autowired
    private SysOperationLogService logService;

    /**
     * 列表页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "list.html";
    }

    /**
     * 查询列表
     */
    @PostMapping("/list")
    @Permission
    @ResponseBody
    public Object list(HttpServletRequest request) {
        Map<String, Object> params = WebUtil.getRequestMap(request);
        PageHelper.startPage(PageUtil.getPageNum(params), PageUtil.getPageSize(params));
        List<SysOperationLog> list = logService.findList(params);
        PageInfo<SysOperationLog> pageInfo = new PageInfo<SysOperationLog>(list);
        return AjaxResult.success((int) pageInfo.getTotal(), list);
    }

    @PostMapping("/detail")
    @Permission
    @ResponseBody
    public Object seeDetails(Long id) {
        if (OftenUtil.isNotEmpty(id)) {
            SysOperationLog operationLog = logService.get(id);
            return AjaxResult.success(operationLog);
        }
        return AjaxResult.error("参数异常");
    }

    @PostMapping("/clear")
    @Permission
    @ResponseBody
    public Object clearLog() {
        logService.deleteTable();
        return AjaxResult.success();
    }
}
